import axios from '~plugins/axios';
import {setToken, unsetToken} from '~plugins/auth';


export const state = {
    userInfo: null
};

export const mutations = {
    SET_USER: function (state, user) {
        state.userInfo = user;
    },
    UPDATE_USER: function (state) {
        state.userInfo = sessionStorage.getItem('user');
    }
};

export const actions = {

    // 账号登录
    username({commit}, params) {
        return axios.post('/api/login/username', params).then((res) => {
            setToken(res.data);
            // commit('SET_USER', res);
        });
    },
    // 手机登录
    mobile({commit}, {mobile, password}) {
        return axios.post('/api/login/mobile', {mobile, password}).then((res) => {
            //   let user = {name: res.data};
            setToken(res);
            // commit('SET_USER', user);
        });
    },
    // 邮箱登录
    email({commit}, {email, password}) {
        return axios.post('/api/login/email', {email, password}).then((res) => {
            setToken(res)
        });
    },
    logout({commit}) {
        unsetToken();
    }
};

export const getters = {
    userInfo(state) {
        console.log();
        return state.userInfo;
    }
};
